package game.base;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

public class SpriteAnimated {
	
	private Point location;
	private Point size;
	private Rect rect;
	public int currentFrame;
	private long frameTicker;
	private int framePeriod;
	private int frames;
	ImagePlus img;
	Paint paint;
	
	public SpriteAnimated(Point location_, Point size_, ImagePlus img_,int fps, int frames_){
		location =location_;
		img = img_;
		size = size_;
		currentFrame = 0;
		frames = frames_;
		size.x = img.getImage().getWidth() / frames_;
		size.y = img.getImage().getHeight();
		rect = new Rect(0, 0, size.x, size.y);
		framePeriod = 1000 / fps;
		frameTicker = 0l;

	}
	
	
	public void draw(Canvas canvas){
		
		 Rect destRect = new Rect(location.x, location.y, location.x + size.x, location.y + size.y);
		 canvas.drawBitmap(img.getImage(), rect, destRect, paint);
	}
	
	public void update(long difftime) {

		if (difftime > frameTicker + framePeriod) {
		        frameTicker = difftime;
		        currentFrame++;	
		        if (currentFrame >= frames) {
		            currentFrame = 0;
		        }
		}
		    this.rect.left = currentFrame * size.x;
		    this.rect.right = this.rect.left + size.y;

	}

																																

}
